<?php
namespace app\admin\controller;
use think\Db;
class Customer extends Base{
    public function customer_list(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('customer')->where('state','<',3)->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('cname|account|url','like',"%{$key}%")->order('id desc')->paginate(10);
        $lists=Db::name('customer')->where('state','<',3)->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('cname|account|url','like',"%{$key}%")->select();
        $data=$list->all();
        
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function customer_add(){
        if(request()->isGet()){
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['cname']=$param['cname'];
            $data['account']=$param['account'];
            $data['url']=$param['url'];
            $data['state']=1;
            $data['time']=time();
            $data['ctoken']=md5('tmd'.$param['account'].$data['time']);
            $customer=Db::name('customer')->where('account',$param['account'])->find();
            if($customer){
                return json(['code' => 500,'msg' => '账号同名']);
            }else{
                $res=Db::name('customer')->insert($data);
                if($res){
                    return json(['code' => 200,'msg' => '成功']);
                }else{
                    return json(['code' => 500,'msg' => '失败']);
                }
            }
        }
    }
    public function customer_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $user=Db::name('customer')->where('id',$param['id'])->find();
            $this->assign('user',$user);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            // $data['cname']=$param['cname'];
            // $data['account']=$param['account'];
            $data['url']=$param['url'];
            $data['state']=1;
            $data['uptime']=time();
            $res= Db::name('customer')->where(array('id'=>$param['id']))->update($data);
            if($res){
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function customer_del(){
        $param = input('post.');
        $res= Db::name('customer')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function customer_dels(){
        $param = input('post.');
        $res= Db::name('customer')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function excel() {
        $result=Db::name('customer')->select();
        for ($i=0; $i <count($result) ; $i++) { 
            $result[$i]['time']=date('Y-m-d H:i:s',$result[$i]['time']);
            $result[$i]['uptime']=date('Y-m-d H:i:s',$result[$i]['uptime']);
        }
        $xlsCell  = array(
            array('id','id'),
            array('cname','cname'),
            array('account','account'),
            array('password','password'),
            array('ctoken','ctoken'),
            array('url','url'),
            array('time','time'),
            array('uptime','uptime'),
            array('state','state')
        );
        $this->exportexcel('客户url',$xlsCell,$result);
    }
    public static function exportexcel($expTitle, $expCellName, $expTableData) {
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        $fileName = $xlsTitle;//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);
        $dataNum = count($expTableData);

        $objPHPExcel = new \PHPExcel();
        $cellName = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ');
        // Miscellaneous glyphs, UTF-8
        for ($i = 0; $i < $dataNum; $i++) {
            for ($j = 0; $j < $cellNum; $j++) {
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + 1), $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        header('pragma:public');
        header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }
}
